'use strict';
import { Service } from 'egg';

export interface NewsItem {
    id: string;
    author_id: string;
    tab: string;
    content: string;
    title: string;
    last_reply_at: string;
    good: boolean;
    top: boolean;
    reply_count: number;
    visit_count:number;
    create_at:string;
    author:object;
}

/**
 * HackerNews Api Service
 */
export class HackerNews extends Service {
  /**
   * request hacker-news api
   * @param api - Api name
   * @param opts - urllib options
   */
  public async request(api: string, opts?: object) {
    const options = Object.assign({
      dataType: 'json',
      timeout: ['30s', '30s'],
    }, opts);

    const result = await this.ctx.curl(`${this.config.news.serverUrl}/${api}`, options);
    return result.data;
  }

  /**
   * get top story ids
   * @param page - page number, 1-ase
   * @param pageSize - page count
   */
  public async getTopStories(page?: number, pageSize?: number): Promise<number[]> {
    page = page || 1;
    pageSize = pageSize || this.config.news.pageSize;

    try {
      const result = await this.request(`topics`, {
        data: {
            page,
            tab: '',
            limit: '20',
        },
      });
      let resultData = [];
      if (result.success) {
        resultData = result.data;
      }
      return Object.keys(resultData).map((key) => resultData[key]);
    } catch (e) {
      this.ctx.logger.error(e);
      return [];
    }
  }

//   /**
//    * query item
//    * @param id - itemId
//    */
//   public async getItem(id: number): Promise<NewsItem> {
//     return await this.request(`item/${id}.json`);
//   }

//   /**
//    * get user info
//    * @param id - userId
//    */
//   public async getUser(id: number) {
//     return await this.request(`user/${id}.json`);
//   }
}

export default HackerNews;
